package com.norteksoft.erm.model;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;


/**
 * @hibernate.class table="ERM_GROUP"
 */
public class Group extends BaseObject{

	private static final long serialVersionUID = -8700444972460706639L;

	/**
	 * 部门路径分隔符
	 */
	public static final String PATH_SEPARATOR = "-";

	/**
	 * 虚拟根code
	 */
	public final static String VIRTUAL_ROOT_CODE = "GROUP_VIRTUAL_ROOT";

	/**
	 * 特殊部门
	 */
	public final static String SPECIAL_GROUP_CODE = "otherGroup";

	private Long groupID = new Long(-1);// OID

	private String groupName;// 部门简称

	private String memo;// 备注

	private Group parentGroup;// 父部门

	private List childGroups = new ArrayList();// 子部门集

	private List users = new ArrayList();// 部门用户集

	private List rights;// 部门权限集（北科工作流专用）

	// ~~ add by Jiffy ===================================

	private String code;// 部门编号(必须唯一)

	private String fullName;// 部门全称

	private String fullPath;// 部门的完整路径 12-23-543

	private String hasChild;// 是否拥有下级部门 true/false

	private Set localRoles = new HashSet();// 局部角色集合

	private String asManageDomain;// 作为一个管理域？

	private String allowDomainRole;// 是否允许创建管理域内部角色？本属性依赖于asManageDomain

	private Integer displayOrder;// 显示排序

	private Integer state;// 组织机构状态

	private String director;// 部门主管用户帐号

	public static final Integer STATE_NORLE = new Integer(1);

	public static final Integer STATE_DELETE = new Integer(0);

	// ~~ 遗留或未知属性 ====================================

	private String groupNum;// ?

	private String groupType;// ?

	public String toString() {
		return new ToStringBuilder(this).append("groupID", groupID).append(
				"groupName", groupName).append("memo", memo).append("code",
				code).append("fullName", fullName).append("fullPath", fullPath)
				.append("asManageDomain", asManageDomain).append(
						"displayOrder", displayOrder).append("state", state)
				.append("groupNum", groupNum).append("groupType", groupType)
				.append("allowDomainRole", allowDomainRole).toString();
	}

	public Group() {
		super();
	}

	/**
	 * @hibernate.id generator-class="sequence" column="GROUPID"
	 *               unsaved-value="-1" type="long"
	 * @hibernate.generator-param name="sequence" value="ERM_SEQUENCE"
	 */

	public Long getGroupID() {
		return groupID;
	}

	public void setGroupID(Long groupID) {
		this.groupID = groupID;
	}

	/**
	 * @hibernate.property column="GROUPNAME" length="90" type="string"
	 * @return String
	 */
	public String getGroupName() {
		return groupName;
	}

	public void setGroupName(String groupName) {
		this.groupName = groupName;
	}

	/**
	 * @hibernate.property column="MEMO" length="1000" type="string"
	 * @return String
	 */
	public String getMemo() {
		return memo;
	}

	public void setMemo(String memo) {
		this.memo = memo;
	}

	/**
	 * @hibernate.many-to-one name="parentGroup" column="PARENTID"
	 *                        cascade="none" outer-join="auto"
	 */
	public Group getParentGroup() {
		return parentGroup;
	}

	public void setParentGroup(Group parentGroup) {
		this.parentGroup = parentGroup;
	}

	/**
	 * @hibernate.bag name="childGroups" cascade="all" lazy="true"
	 *                inverse="true" order-by="groupName asc"
	 * @hibernate.key column="PARENTID"
	 * @hibernate.one-to-many class="com.norteksoft.erm.model.Group"
	 * @return List
	 */
	public List getChildGroups() {
		return childGroups;
	}

	public void setChildGroups(List childGroups) {
		this.childGroups = childGroups;
	}

	/**
	 * @hibernate.bag name="users" table="ERM_USER_GROUP" cascade="save-update"
	 *                lazy="true" inverse="true" outer-join="auto"
	 * @hibernate.key column="GROUPID"
	 * @hibernate.many-to-many class="com.norteksoft.erm.model.User"
	 *                         column="USERID"
	 * @return List
	 */
	public List getUsers() {
		return users;
	}

	public void setUsers(List users) {
		this.users = users;
	}

	/**
	 * @hibernate.property column="CODE" length="30" type="string"
	 *                     not-null="true"
	 * @return String
	 */
	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	/**
	 * @hibernate.property column="FULLNAME" length="150" type="string"
	 * @return String
	 */
	public String getFullName() {
		return fullName;
	}

	public void setFullName(String fullName) {
		this.fullName = fullName;
	}

	/**
	 * @hibernate.bag name="rights" cascade="all-delete-orphan" lazy="true"
	 *                inverse="true"
	 * @hibernate.key column="GROUPID"
	 * @hibernate.one-to-many class="com.norteksoft.erm.model.GroupRight"
	 * @return List
	 */
	public List getRights() {
		return rights;
	}

	public void setRights(List rights) {
		this.rights = rights;
	}

	/**
	 * @hibernate.property column="GROUPNUM" length="30" type="string"
	 * @return String
	 */
	public String getGroupNum() {
		return groupNum;
	}

	public void setGroupNum(String groupNum) {
		this.groupNum = groupNum;
	}

	/**
	 * @hibernate.property column="GROUPTYPE" length="30" type="string"
	 * @return String
	 */

	public String getGroupType() {
		return groupType;
	}

	public void setGroupType(String groupType) {
		this.groupType = groupType;
	}

	/**
	 * @hibernate.property column="FULLPATH" length="500" type="string"
	 * @return String
	 */
	public String getFullPath() {
		return fullPath;
	}

	public void setFullPath(String fullPath) {
		this.fullPath = fullPath;
	}

	/**
	 * @hibernate.set cascade="all-delete-orphan" inverse="true" lazy="true"
	 *                order-by="specialType,roleName" name="roles"
	 * @hibernate.key column="GROUPID"
	 * @hibernate.one-to-many class="com.norteksoft.erm.model.Role"
	 * @return
	 */
	public Set getLocalRoles() {
		return localRoles;
	}

	public void setLocalRoles(Set localRoles) {
		this.localRoles = localRoles;
	}

	/**
	 * @hibernate.property column="ASMANAGEDOMAIN" length="10" type="string"
	 * @return String
	 */

	public String getAsManageDomain() {
		return asManageDomain;
	}

	public void setAsManageDomain(String asManageDomain) {
		this.asManageDomain = asManageDomain;
	}

	/**
	 * 通过部门分隔符获得上级所有部门ID
	 * 
	 * @return
	 */
	public String[] getUpGroups() {
		if (StringUtils.isEmpty(this.fullName)) {
			return null;
		} else {
			return this.fullPath.split(Group.PATH_SEPARATOR);
		}
	}

	/**
	 * 获得所有上级部门OID以及自身的OID
	 * 
	 * @return
	 */
	public Set getSeniorGroupOIDs() {
		Set result = new HashSet();
		if (StringUtils.isNotBlank(fullPath)) {
			String[] groupOIDs = StringUtils.split(fullPath,
					Group.PATH_SEPARATOR);
			CollectionUtils.addAll(result, groupOIDs);
		}
		return result;
	}

	/**
	 * getNodeID
	 * 
	 * @return String
	 */
	public String getNodeID() {
		return this.getGroupID().toString();
	}

	/**
	 * getNodeName
	 * 
	 * @return String
	 */
	public String getNodeName() {
		return this.getGroupName();
	}

	public String getParentID() {
		if (this.getParentGroup() != null && !this.getParentGroup().equals("")) {
			return this.getParentGroup().getGroupID().toString();
		} else {
			return "";
		}
	}

	public boolean isManageDomain() {
		return "true".equals(this.asManageDomain);
	}

	/**
	 * @hibernate.property column="DISPLAYORDER" type="integer"
	 * @return Integer
	 */
	public Integer getDisplayOrder() {
		return displayOrder;
	}

	public void setDisplayOrder(Integer displayOrder) {
		this.displayOrder = displayOrder;
	}

	/**
	 * @hibernate.property column="STATE" type="integer"
	 * @return Integer
	 */
	public Integer getState() {
		return state;
	}

	public void setState(Integer state) {
		this.state = state;
	}

	/**
	 * @hibernate.property column="ALLOWDOMAINROLE" length="10" type="string"
	 * @return String
	 */
	public String getAllowDomainRole() {
		return allowDomainRole;
	}

	public void setAllowDomainRole(String allowDomainRole) {
		this.allowDomainRole = allowDomainRole;
	}

	/**
	 * @hibernate.property column="HASCHILD" length="10" type="string"
	 * @return String
	 */
	public String getHasChild() {
		return hasChild;
	}

	public void setHasChild(String hasChild) {
		this.hasChild = hasChild;
	}

	/**
	 * @hibernate.property column="DIRECTOR" length="60" type="string"
	 * @return String
	 */

	public String getDirector() {
		return director;
	}

	public void setDirector(String director) {
		this.director = director;
	}

}
